package com.fanxl.flink.datastream.source;

import com.fanxl.flink.datastream.transformation.Access;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;

import java.util.Properties;

/**
 * @description:
 * @author: fanxiaole
 * @date: 2022/1/11 16:18
 */
public class SourceApp {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
//        fileSource(env);
//        customSource(env);
//        mysqlSource(env);
//        socketSource(env);

        env.execute("SourceApp");
    }

    public static void kafkaSource(StreamExecutionEnvironment env) {
        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "10.215.7.21:9092");
        properties.setProperty("group.id", "test-consumer-group");
        DataStream<String> stream = env
                .addSource(new FlinkKafkaConsumer<>("flink-test", new SimpleStringSchema(), properties));

        System.out.println(stream.getParallelism());

        stream.print();
    }

    public static void mysqlSource(StreamExecutionEnvironment env ) {
        DataStreamSource<Student> source = env.addSource(new StudentSource()).setParallelism(1);
        System.out.println(source.getParallelism());
        source.print();
    }

    public static void customSource(StreamExecutionEnvironment env) {
        DataStreamSource<Access> accessStream = env.addSource(new AccessSource());
        accessStream.print();
    }

    public static void fileSource(StreamExecutionEnvironment env) {
        SingleOutputStreamOperator<String> source = env.readTextFile("data/word.data");
        source.print();
    }

    public static void socketSource(StreamExecutionEnvironment env) {
        // 对接数据源的数据
        DataStreamSource<String> source = env.socketTextStream("10.215.15.200", 9527);
        source.print();
    }
}
